package com.neighbor.neighborhoodadmin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.neighbor.neighborhoodadmin.pojo.Posts;
import com.neighbor.neighborhoodadmin.service.IPostsService;
import com.neighbor.neighborhoodadmin.service.IUsersService;
import com.neighbor.neighborhoodadmin.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户帖子表 前端控制器
 * </p>
 *
 * @author song
 * @since 2025-09-22 09:11:23
 */
@RestController

public class PostsController {
    @Autowired
    private IPostsService postService;
    @Autowired
    private IUsersService usersService;

    /**
     * 查询所有
     * @param page 页码
     * @param pageSize 每页大小
     * @param type 帖子类型
     * @param status 帖子状态
     * @param nickname 用户名,用于搜索用
     * @return ResponseMsg
     */
    @GetMapping("/posts")
    public ResponseMsg posts(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "5")Integer pageSize,
                             Integer type,
                             Integer status,
                             Integer postId,
                             String nickname) {
        // 1. 参数校验和默认值设置
        // 如果页码或每页大小为空，则设置默认值
        if (page == null || page < 1) {
            page = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 10;
        }

        // 2. 调用服务层方法获取分页数据
        IPage<Posts> postPage = postService.getPosts(page, pageSize, type, status, postId, nickname);

        // 3. 封装并返回响应
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS,"成功获取帖子列表", postPage);
    }


    /**
     * @Description 根据帖子ID获取帖子详情
     * @param postId 路径变量，帖子ID
     * @return ResponseMsg 包含帖子详情的响应消息，如果找不到则返回失败消息
     */
    @GetMapping("posts/{postId}")
    public ResponseMsg getPost(@PathVariable Integer postId) {
        if (postId == null) {
            return ResponseMsg.getInstance(ResponseMsg.PARAMETER_ERROR,"帖子ID不能为空");
        }
        return postService.getPostById(postId);
    }

    /**
     * @Description 根据帖子ID更新帖子状态
     * @param postId 路径变量，帖子ID
     * @param status 帖子状态（例如：active, hidden, reported）
     * @return ResponseMsg 包含操作结果的响应消息
     */
    @PutMapping("posts/{postId}/status")
    public ResponseMsg updatePostStatus(@PathVariable Integer postId, Integer status) {

        if (postId == null || status == null) {
            return ResponseMsg.getInstance(ResponseMsg.PARAMETER_ERROR,"帖子ID或状态不能为空");
        }
        return postService.updatePostStatus(postId, status);
    }


    /**
     * @Description 根据帖子ID删除帖子
     * @param postId 路径变量，帖子ID
     * @return ResponseMsg 包含操作结果的响应消息
     */
    @DeleteMapping("posts/{postId}")
    public ResponseMsg delPost(@PathVariable Integer postId) {
        if (postId == null) {
            return ResponseMsg.getInstance(ResponseMsg.PARAMETER_ERROR,"帖子ID不能为空");
        }
        return postService.deletePost(postId);
    }



}
